import { useState } from "react";
import { useModel } from "umi";
import { message, Modal } from "antd";

const useVersionOnline = () => {
    const { remoteData } = useModel("global");
    const { baseInfo } = useModel("person.info");

    const [submitLoading, setSubmitLoading] = useState(false);

    const handleVersionOnline = async (operatorCenter, application, clientType, clientVersion, reloadData) => {
        let modal = Modal.info({
            content: "正在上线应用信息......",
            okButtonProps: { loading: true },
        });
        setSubmitLoading(true);
        const { appUuid } = application;
        const { apvCodesize, apvDesc, apvHash, apvPubtime, apvType, apvVersion } = clientVersion;
        let res = await remoteData({
            object: "bnt-application-operation-center-service",
            serviceUrl: operatorCenter.url,
            serviceName: "/developer/person/application/versionOnline",
            params: { application, clientType, clientVersion },
        });
        if (res) {
            let params = { appUuid, apvVersion, apvCodesize, apvHash, apvPubtime, apvType };
            res = await remoteData({
                object: "bnt-application-resource-center-service",
                serviceName: "/developer/person/application/typeVersionOnlined",
                params: {
                    ...params,
                    apvOlti: new Date().getUTCTime(),
                    acUrl: operatorCenter.url,
                    atvoOnui: baseInfo.uuid,
                    atvoOnam: baseInfo.realName,
                    appVdesc: apvDesc,
                    runEnv: operatorCenter.runEnv,
                },
            });
            if (res) {
                message.success("应用版本上线成功");
                reloadData && reloadData();
            }
        }
        modal.destroy();
        setSubmitLoading(false);
    };

    return { submitLoading, setSubmitLoading, handleVersionOnline };
};

export default useVersionOnline;
